package com.xintong.visualinspection.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xintong.visualinspection.bean.Menu;
import com.xintong.visualinspection.bean.Organ;
import com.xintong.visualinspection.dao.cluster.DepartmentDao;
import com.xintong.visualinspection.service.BaseService;
import com.xintong.visualinspection.service.DepartmentService;

import lombok.Data;

/**
 * 文件名：UserServiceImpl 版本信息：日期：2017/3/30 Copyright 江苏省交通规划设计院 Corporation 2017
 * 版权所有.
 */
@Service
@Data
public class DepartmentServiceImpl extends BaseService implements DepartmentService {

	private static final org.slf4j.Logger logger = LoggerFactory.getLogger(DepartmentService.class);

	@Autowired
	private DepartmentDao departmentDao;

	@Override
	public List<Organ> getOrgans(Organ organ) {

		List<Organ> organs = departmentDao.getOrgan(organ);
		List<Organ> olist = new ArrayList<>();
		Map<Integer, List<Organ>> map = new HashMap<>();
		// 找出所有父
		for (Organ o : organs) {
			if (o.getParentid() != null && o.getParentid()!=0) {
				List<Organ> organList = map.get(o.getParentid());
				if (organList == null) {
					organList = new ArrayList<>();
				}
				organList.add(o);
				map.put(o.getParentid(), organList);
			}
		}
		// 找出子
		for (Integer oid : map.keySet()) {
			for (Organ m2 : organs) {
				if (m2.getId() == oid) {
					m2.setChilds(map.get(oid));
					if (m2.getParentid() == null || m2.getParentid()==0) {
						olist.add(m2);
					}
				}
			}
		}

		return olist;
	}

	@Override
	public void insert(Organ organ) {
		departmentDao.insert(organ);

	}

	@Override
	public void update(Organ organ) {
		departmentDao.update(organ);
	}

	@Override
	public void delete(Integer id) {
		departmentDao.delete(id);
	}

}
